//
//  changePwdViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/14.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "changePwdViewController.h"
#import "forgetViewController.h"

@interface changePwdViewController ()

@property(nonatomic,strong) UITextField *oldPwdTextField;
@property(nonatomic,strong) UITextField *PwdTextField;
@property(nonatomic,strong) UITextField *PwdTextField2;

@end

@implementation changePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark ------------------ init -------------------

- (void)initView{
    
    self.title = @"修改密码";
    self.view.backgroundColor = getColor(@"f5f5f5");
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(60), screenScale(40))];
    rightBtn.titleLabel.font = get4sFont(14);
    rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, screenScale(10), 0, screenScale(-10));
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:getColor(@"4a4a4a") forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItme;
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(134.5))];
    bgView.backgroundColor = getColor(@"ffffff");
    [self.view addSubview:bgView];
    
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1), scrWidth(), screenScale(0.7))];
    threadView.backgroundColor = getColor(@"cccccc");
    [self.view addSubview:threadView];
    
    ///
    
    UILabel *oldPwdLabel = [ConfigXp labelWithFrame:CGRectZero
                                               text:@"旧密码"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [self.view addSubview:oldPwdLabel];
    [oldPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(15));
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        
        
    }];
    
    _oldPwdTextField = [[UITextField alloc]init];
    _oldPwdTextField.placeholder = @"请输入旧密码";
    _oldPwdTextField.textColor = getColor(@"4a4a4a");
    _oldPwdTextField.secureTextEntry = YES;
    _oldPwdTextField.font = get4sFont(12);
    _oldPwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_oldPwdTextField];
    [_oldPwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(17));
        make.left.mas_equalTo(self.view).with.offset(screenScale(85));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    UIView *oldThreadView = [[UIView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(44), scrWidth() - screenScale(15), screenScale(0.5))];
    oldThreadView.backgroundColor = getColor(@"eeeeee");
    [self.view addSubview:oldThreadView];
    
    ///
    
    UILabel *newPwdLabel = [ConfigXp labelWithFrame:CGRectZero
                                               text:@"新密码"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [self.view addSubview:newPwdLabel];
    [newPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(60));
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        
        
    }];
    
    _PwdTextField = [[UITextField alloc]init];
    _PwdTextField.placeholder = @"6-16位字母和数字";
    _PwdTextField.textColor = getColor(@"4a4a4a");
    _PwdTextField.font = get4sFont(12);
    _PwdTextField.secureTextEntry = YES;
    _PwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_PwdTextField];
    [_PwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(62));
        make.left.mas_equalTo(self.view).with.offset(screenScale(85));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    UIView *newThreadView = [[UIView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(89), scrWidth() - screenScale(15), screenScale(0.5))];
    newThreadView.backgroundColor = getColor(@"eeeeee");
    [self.view addSubview:newThreadView];
    
    ///
    
    UILabel *newPwdLabel1 = [ConfigXp labelWithFrame:CGRectZero
                                               text:@"重复密码"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [self.view addSubview:newPwdLabel1];
    [newPwdLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(105));
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        
        
    }];
    
    _PwdTextField2 = [[UITextField alloc]init];
    _PwdTextField2.placeholder = @"重复输入新密码";
    _PwdTextField2.textColor = getColor(@"4a4a4a");
    _PwdTextField2.font = get4sFont(12);
    _PwdTextField2.secureTextEntry = YES;
    _PwdTextField2.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_PwdTextField2];
    [_PwdTextField2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(107));
        make.left.mas_equalTo(self.view).with.offset(screenScale(85));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    ///
    
    UIView *bgView2 = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(142.5), scrWidth(), screenScale(43.5))];
    bgView2.backgroundColor = getColor(@"ffffff");
//    [self.view addSubview:bgView2];
    
    UIButton *forgetBtn = [ConfigXp buttonWithFrame:CGRectMake(0, screenScale(142.5), scrWidth(), screenScale(43.5))
                                              title:nil
                                         titleColor:nil
                                               font:nil
                                    backgroundColor:nil
                                              image:nil];
    forgetBtn.backgroundColor = getColor(@"ffffff");
    [forgetBtn addTarget:self action:@selector(forgetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    
    UILabel *forgetPwd = [ConfigXp labelWithFrame:CGRectZero
                                                text:@"忘记密码"
                                           textColor:getColor(@"4a4a4a")
                                                font:get4sFont(13)
                                       textAlignment:NSTextAlignmentLeft
                                     backgroundColor:[UIColor clearColor]];
    [forgetBtn addSubview:forgetPwd];
    [forgetPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(forgetBtn).with.offset(screenScale(12));
        make.left.mas_equalTo(forgetBtn).with.offset(screenScale(15));
        
        
    }];
    
    UIImageView *rightImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_icon"]];
    [forgetBtn addSubview:rightImg];
    [rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(forgetBtn).with.offset(screenScale(15));
        make.right.mas_equalTo(forgetBtn).with.offset(screenScale(-15));
        
        
    }];
}

#pragma mark ------------------ click -------------------

- (void)forgetBtnClick{

    NSLog(@"1");
    forgetViewController *vc = [[forgetViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)rightBtnClick{
    
    if (![TSRegularExpressionUtil validatePassword:_PwdTextField.text] || ![TSRegularExpressionUtil validatePassword:_PwdTextField2.text]) {
        
        [PublicFunction showAlert:nil message:@"密码长度为6-16位" duration:1.0];
        return;
    }
    
    if ([_PwdTextField.text isEqualToString:_PwdTextField2.text] == NO) {
        
        [PublicFunction showAlert:nil message:@"两次输入不相同！" duration:1.0];
        return;
        
    }
    
    [self.view endEditing:YES];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue: _oldPwdTextField.text forKey:@"old_pass"];
    [productParams setValue: _PwdTextField.text forKey:@"new_pass"];
    [productParams setValue: _PwdTextField2 forKey:@"new_agreen_pass"];
    
    [[MNetworkHandler sharedInstance]conURL:more_editPassword
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
    
}

@end
